/** This code is under the GPL, viewable at http://www.gnu.org/copyleft/gpl.html
* You are free to modify and redistribute the code as you see fit, but three main
* conditions exist for usage of this code:
* 1. You may only pass on this code with the same rights and priveleges that were given
* to you (In other words, you must redistribute this license)
* 2. You may NOT distribute this code for sale or for profit.
* 3. There are no warranties about this code. It is designed not to screw up your computer,
* but in the case that it does, the writer cannot be held responsible for losses.
*
* Code author: flug_m (post comments/questions to http://www.saveourtunes.com/forum/ In future
* versions, I'll make available alternative means of contact.)
* Code Revision date: August 21, 2007
* Code Revision version: 0.4
*
* The OurTunesFrame is the high-level container that presents the GUI. It
* will display itself as 1/4 the total screen size and centered on screen.
* Since every monitor has a different resolution, a future version will
* specify the size of the frame exactly.
*
* All of the components are drawn into the OurTunesPanel object.
*/
package ourtunes.gui;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import ourtunes.Shared;
import ourtunes.about.AboutFrame;
public class OurTunesFrame extends JFrame
{
public OurTunesFrame()
{
setTitle("ourTunes build 1.7a");
setResizable(false);
//Center the frame on screen.
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (int)(d.width) / 4;
int y = (int)(d.height) / 4;
setLocation(x,y);
setSize(d.width/2, d.height/2);
//Build the MenuBar
buildMenu();
}
public void buildMenu()
{
JMenuBar menuBar = new JMenuBar();
JMenu options = new JMenu("Options");
JMenuItem view = new JMenuItem("View Services");
JMenuItem interrupt = new JMenuItem("Interrupt Pass-through");
JMenuItem about = new JMenuItem("About");
JMenuItem exit = new JMenuItem("Exit");
options.add(about);
options.add(view);
options.add(interrupt);
options.add(exit);
menuBar.add(options);
exit.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
int choice = JOptionPane.showConfirmDialog(OurTunesFrame.this,
"Are you sure you" +
" want to exit?", "Exit", JOptionPane.YES_NO_OPTION);
if (choice == JOptionPane.YES_OPTION) System.exit(0);
}
});
about.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Point p = OurTunesFrame.this.getLocation();
if (p.x >= 600 || p.x<0) p.x = 400;
if (p.y >= 800 || p.y<0) p.y = 400;
JFrame f = new AboutFrame();
f.setLocation(p.x, p.y);
f.setVisible(true);
}
});
view.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Point p = OurTunesFrame.this.getLocation();
if (p.x >= 600 || p.x<0) p.x = 400;
if (p.y >= 800 || p.y<0) p.y = 400;
try{
//Note: I made the constructor to this class public, and added
//a few known services to the list.
JFrame browser = new com.strangeberry.jmdns.tools.Browser(Shared.getJmDNSInstance());
browser.setLocation(p.x, p.y);
browser.setVisible(true);
}
catch(Exception e)
{
e.printStackTrace();
JOptionPane.showMessageDialog(OurTunesFrame.this, "Could not launch the Bonjour discovery."+
" launch ourTunes from the console for more info.");
}
}
});
interrupt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
Thread[] arr = new Thread[Thread.activeCount()+1];
Thread.enumerate(arr);
for (Thread t : arr)
{
if (t == null) continue;
if (t.getName().startsWith("Pass-through"))
{
System.out.println(t);
t.interrupt();
}
}
}
});
setJMenuBar(menuBar);
}
public void addPanel(JPanel panel)
{
getContentPane().add(panel);
}
}